#!/bin/bash
# 功能：配置双主MySQL
# 前提：需要配置免密,当前主机到master1 master2 ssh免密即可

source comm.cfg

color_text() {
    local color=$1
    local text=$2
    echo -e "\e[${color}m${text}\e[0m"
}

error() {
    color_text "31" "$1"
}

info() {
    color_text "32" "$1"
}

function installmaster1() {
# 更新包列表
sudo apt-get update
# 安装MySQL Server
sudo apt-get install -y mysql-server=8.0.*
# 设置MySQL root密码
info "Setting the root password..."
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY \"${MYSQL_ROOT_PASSWORD}\";"
sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY \"${MYSQL_ROOT_PASSWORD}\";"
sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;"
sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "FLUSH PRIVILEGES;"

cp ${INSTALLDIR}/master1-my.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
systemctl restart mysql

if [ $? -ne 0 ];then 
    error "MySQL install failed"
    exit 1
else
    info "MySQL has been installed and secured."
fi
}

function installmaster2() {
# 更新包列表
sudo apt-get update
# 安装MySQL Server
sudo apt-get install -y mysql-server=8.0.*
# 设置MySQL root密码
info "Setting the root password..."
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY \"${MYSQL_ROOT_PASSWORD}\";"
sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY \"${MYSQL_ROOT_PASSWORD}\";"
sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;"
sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "FLUSH PRIVILEGES;"
cp ${INSTALLDIR}/master2-my.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
systemctl restart mysql

if [ $? -ne 0 ];then 
    error "MySQL install master2 failed"
    exit 1
else
    info "MySQL master2 has been installed"
fi
}

dbready() {
    for i in $(seq 1 10)
    do
        nc -zv $1 3306 &> /dev/null
        if [ $? -ne 0 ];then
            error "Waiting Ready......"
            sleep 10
        else
            info "$1 Mysql Is Ready"
            return 0
        fi
    done
    error "Waiting Mysql Timeout"
    return 1
}

# 配置双主
msconfig() {
ip a |grep ${MASTER1}
if [ $? -eq 0 ];then
    # 配置master1
    installmaster1
    dbready ${MASTER2}
    if [[ $? -eq 0 ]];then
        #log_file_1=`mysql -uroot -h${MASTER2} -p${MYSQL_ROOT_PASSWORD} -e 'show master status'|sed '4p'|sed '1d'|awk '{print $1}'`
        #log_pos_1=`mysql -uroot -h${MASTER2} -p${MYSQL_ROOT_PASSWORD} -e 'show master status'|sed '4p'|sed '1d'|awk '{print $2}'`
	#echo $log_file_1 $log_pos_1
        #mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "change master to master_host=\"${MASTER1}\",master_port=3306,master_user='root',master_password=\"${MYSQL_ROOT_PASSWORD}\",master_log_file=\"${log_file_1}\",master_log_pos=${log_pos_1};"
        mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "change master to master_host=\"${MASTER2}\",master_port=3306,master_user='root',master_password=\"${MYSQL_ROOT_PASSWORD}\";"
	    if [ $? -eq 0 ];then
            mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "start slave;"
            info "Change Master To ${MASTER2} Succeed"
        else
            error "Change Master To ${MASTER2} Failed"
        fi
    fi
else
    # 配置master2
    installmaster2
    dbready ${MASTER1}
    if [[ $? -eq 0 ]];then
        #log_file_2=`mysql -uroot -h${MASTER1} -p${MYSQL_ROOT_PASSWORD} -e "show master status"|sed '4p'|sed '1d'|awk '{print $1}'`
        #log_pos_2=`mysql -uroot -h${MASTER1} -p${MYSQL_ROOT_PASSWORD} -e "show master status"|sed '4p'|sed '1d'|awk '{print $2}'`
        #echo $log_file_2 $log_pos_2
        #mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "change master to master_host=\"${MASTER1}\",master_port=3306,master_user='root',master_password=\"${MYSQL_ROOT_PASSWORD}\",master_log_file=\"${log_file_2}\",master_log_pos=${log_pos_2};"
        mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "change master to master_host=\"${MASTER1}\",master_port=3306,master_user='root',master_password=\"${MYSQL_ROOT_PASSWORD}\";"
	    if [ $? -eq 0 ];then
            mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "start slave;"
            info "Change Master To ${MASTER1} Succeed"
        else
            error "Change Master To ${MASTER1} Failed"
        fi
    fi
fi
}

main() {
    msconfig
}
main
